﻿using System;
using System.Collections.Generic;
using System.Text;



namespace ForumServer.Forum
{
    [Serializable]
    public class Thread : MarshalByRefObject
    {
        private string        _header;
        private string        _content;
        private int           _numOfReplies;
        private int           _numOfViews;
        private User          _creator;
        private DateTime      _publishDate;
        private DateTime      _lastDate;
        private List<Post>    _postList;
        

        public Thread(string header,string content, User creatorName)
        {
            _header = header;
            _content = content;
            _numOfReplies = 0;
            _numOfViews = 0;
            _creator = creatorName;
            _publishDate = DateTime.Now;
            _lastDate = DateTime.Now;
            _postList = new List<Post>();
        }
        public string Header { get { return _header; } set { _header = value; } }
        public int NumOfReplies { get { return _numOfReplies; } set { _numOfReplies = value; } }
        public int NumOfViews { get { return _numOfViews; } set { _numOfViews = value; } }
        public DateTime PublishDate { get { return _publishDate; } }
        public DateTime LastDate { get { return _lastDate; } set { _lastDate = value; } }
        public List<Post> PostList { get { return _postList; } }
        public User Creator { get { return _creator; } }
        public string CreatorName { get { return _creator.Username; } }
        public string Content { get { return _content; } set { _content = value; } }


        /// <summary>
        /// add new post to the thread list
        /// </summary>
        /// <param name="p"> the new post </param>
        public void addNewPost(Post p)
        {
            _postList.Add(p);
            _lastDate = DateTime.Now;
        }
        /// <summary>
        /// remove existing post at index i;
        /// </summary>

        public bool removePostAt(int index)
        {
            if (index >= 0 && index < this._postList.Count)
            {
                _postList.RemoveAt(index);
                return true;
            }
            return false;
        }
        /// <summary>
        /// replace the post at the index with p and return true in case the index is valid and false otherwise
        /// </summary>
        /// <param name="index"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool editPostAt(int index, Post p)
        {
            if (index >= 0 && index < this._postList.Count)
            {
                _postList[index] = p;
                return true;
            }
            return false;
        }

        public String getThreadPosts()
        {
            string posts = "";
            foreach (Post p in _postList)
            {
                posts += p.getPostDetails() + '\n';
            }
            return posts;
        }

        public String getThreadDetails()
        {
            string details = "";
            details += _header + '~';
            details += _content + '~';
            details += _numOfReplies.ToString() + '~';
            details += _numOfViews.ToString() + '~';
            details += _creator.Username + '~';
            details += _publishDate.ToString() + '~';
            details += _lastDate;
            return details;
        }
    }
}
